layui.define(['jquery', 'layer', 'setter', 'ax'], function (exports) {

    var $ = layui.jquery
        , $win = $(window), $body = $('body')
        , $ax = layui.ax
        , layer = layui.layer
        , setter = layui.setter
        , container = $('#' + setter.container)
        , view = {
        screen: function () {  // 屏幕判断
            var width = $win.width()
            if (width >= 1200) {
                return 3; //大屏幕
            } else if (width >= 992) {
                return 2; //中屏幕
            } else if (width >= 768) {
                return 1; //小屏幕
            } else {
                return 0; //超小屏幕
            }
        }
        , openIframe: function (config) {
            layer.open($.extend({
                type: 2, title: '提示',
                area: ['800px', '600px'],
                maxmin: true,
                zIndex: 9999999,
                shade: 0.8,
                content: ''
            }, config))
        }
        , openPage: function (config) {
            layer.open($.extend({
                type: 1,
                title: '提示',
                area: ['800px', '600px'],
                maxmin: true,
                zIndex: 9999999,
                shade: 0.8,
                content: ''
            }, config))
        },
        config: {
            _csrf: ''
        },
        events: {
            theme: function (othis, setter, view) {
                var url = othis.attr('data-url');
                if (!url) {
                    layer.alert('请先配置请求地址');
                    return false
                }

                var label = othis.attr('data-label')
                    , winConfig = {title: label, content: url + '?iframe=1'};
                view.openIframe(winConfig)
            }
            , 'reset-password': function (othis, setter, view) {
                layer.prompt({
                    formType: 0, //输入框类型，支持0（文本）默认1（密码）2（多行文本）
                    value: '', //初始时的值，默认空字符
                    maxlength: 140, //可输入文本的最大长度，默认500
                    title: '修改密码'
                }, function (value, index, elem) {

                    var url = othis.attr('data-url'),
                        method = othis.attr('data-method') || 'get'
                    if (!url) {
                        layer.alert('请先配置请求地址');
                        return false
                    }

                    var ajax = new $ax(url, function (res) {
                        layer.close(index);
                    });
                    ajax.setMethod(method);
                    ajax.setData({password: value});
                    ajax.send();
                });
            }
            , flexible: function (othis, setter, view) {

                var app = $('#' + setter.APP)
                    , iconElem = $('#' + setter.APP_FLEXIBLE)
                    , screen = view.screen();

                var status = othis.data('status');

                //设置状态，PC：默认展开、移动：默认收缩
                if (status === 'spread') {
                    //切换到展开状态的 icon，箭头：←
                    iconElem.removeClass(setter.ICON_SPREAD).addClass(setter.ICON_SHRINK);

                    //移动：从左到右位移；PC：清除多余选择器恢复默认
                    if (screen < 2) {
                        app.addClass(setter.APP_SPREAD_SM);
                    } else {
                        app.removeClass(setter.APP_SPREAD_SM);
                    }

                    $('.layui-side').removeClass('yak-side');

                    app.removeClass(setter.SIDE_SHRINK)
                    othis.data('status', 'close')
                } else {
                    //切换到搜索状态的 icon，箭头：→
                    iconElem.removeClass(setter.ICON_SHRINK).addClass(setter.ICON_SPREAD);

                    //移动：清除多余选择器恢复默认；PC：从右往左收缩
                    if (screen < 2) {
                        app.removeClass(setter.SIDE_SHRINK);
                    } else {
                        app.addClass(setter.SIDE_SHRINK);
                    }

                    app.removeClass(setter.APP_SPREAD_SM)

                    $(".layui-side").hasClass('yak-side') || $('.layui-side').addClass('yak-side');
                    othis.data('status', 'spread')
                }
            }
            , "submit-by-iframe": function (othis, setter, view) {
                var form = othis.parents('form')
                var filter = form.attr('lay-filter')
                form.removeAttr('lay-filter')
                form.attr('target', 'yak-iframe')
                var property = othis.attr('yak-property')
                var outputHidden = $('<input type="hidden" name="' + property + '" value="1">')
                form.append(outputHidden)
                form.submit()
                form.attr('lay-filter', filter)
                form.removeAttr('target')
                outputHidden.remove()
            }
        }
    }

    // 核心入口
    view.render = function (config, events) {
        this.config = $.extend(this.config, config)
        this.events = $.extend(this.events, events)
    };

    !function () {
        // 绑定全局 click 事件
        $body.on('click', '*[yak-event]', function () {
            var othis = $(this)
                , attrEvent = othis.attr('yak-event')
                , $events = view.events;
            $events[attrEvent] && $events[attrEvent].call(this, othis, setter, view);
        });


        $body.on('mouseenter', '.layadmin-side-shrink .layui-side li', function () {
            var othis = $(this);

            if (othis.has('.layui-nav-child').length > 0) {
                othis.css({position: 'relative'});
                othis.children('.layui-nav-child')
                    .css({display: 'block', position: 'absolute', left: '50px'});
            } else {
                var tips = othis.children('a').text()
                    , index = layer.tips(tips, this, {
                    tips: 2
                    , time: -1
                    , success: function (layero, index) {
                        layero.css({'margin-left': '0px'});
                    }
                });
                othis.data('index', index);
            }

        }).on('mouseleave', '.layadmin-side-shrink .layui-side li', function () {

            var othis = $(this);
            if (othis.has('.layui-nav-child').length > 0) {
                othis.removeAttr('style')
                othis.children('.layui-nav-child').removeAttr('style')
            } else {
                layer.close($(this).data('index'));
            }

        });

    }();

    exports('view', view)

})